package com.qf.service.impl;

import com.qf.entity.ProductReview;
import com.qf.entity.ProductReviewVO;
import com.qf.mapper.ProductReviewMapper;
import com.qf.service.ProductReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * --- 万物归集  不随你我 ---
 * <p>
 * 快捷短语
 * main ==  public static void main(String[] args);
 * sout ==  System.out.println();
 *
 * @author shuhan
 * @date 2025/5/4
 * @desc
 */
@Service
public class ProductReviewServiceImpl implements ProductReviewService {
    @Autowired
    private ProductReviewMapper productReviewMapper;

    @Override
    public List<ProductReview> findAllReviews() {
        return productReviewMapper.selectAll();
    }

    @Override
    public void deleteReviewById(Integer id) {
        productReviewMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveReviews(ProductReviewVO productReviewVO) {
        productReviewVO.setCreateTime(new Date());
        String userName = productReviewVO.getUserName();
        String productName = productReviewVO.getProductName();
        Long uid = productReviewMapper.selectUserIdByUserName(userName);
        Long pid = productReviewMapper.selectProductIdByProductName(productName);
        productReviewVO.setUserId(uid);
        productReviewVO.setProductId(pid);
        productReviewMapper.insert(productReviewVO);

    }

    @Override
    public void updateReviews(ProductReviewVO productReviewVO) {
        productReviewVO.setUpdateTime(new Date());
        productReviewMapper.updateByPrimaryKey(productReviewVO);
    }
}
